import Vue from 'vue'
import Router, {
    RouterMount
} from '@/js_sdk/hhyang-uni-simple-router';
import Store from '@/core/store/index.js';
import qs from 'qs';
import MyHttp from '@/core/http/index.js'
import config from '@/core/config/index' 

// #ifdef H5
if(config.mode == 1){
	Vue.use(Router);
	const routesConfig = {
	    h5: {
	        paramsToQuery: true,
	        loading: true,
	        vueRouterDev: false,
	        useUniConfig: true,
	        resetStyle: () => {
	            return {
	                style: `
					#router-loadding .loadding {
						background-color: #1079fc !important;
						box-shadow: 0 0 15px #1079fc !important;
					}
					`
	            }
	        }
	    },
	    APP:{
	        loddingPageStyle:()=>JSON.parse('{"backgroundColor":"#96DBF3"}'),
	        loddingPageHook:(view)=>{
	            view.drawBitmap('/static/wait.gif', {}, {
	                top: 'auto',
	                left: 'auto',
	                width: '200px',
	                height: '200px'
	            })
	        },
	        holdTabbarStyle:()=>JSON.parse('{"backgroundColor":"#FF0000","opacity":0.2}')
	    },
	    encodeURI: false,
	    debugger: true,
	    routerBeforeEach:()=>{
	        // console.log('routerBeforeEach')
	    },
	    routerAfterEach:()=>{
	        // console.log('routerAfterEach')
	    },
		 routes:ROUTES //路由表
	
	}
	
	const router = new Router(routesConfig);
	const whitelist = {
	    '/pages/index/index': 'index',
	}
	// console.log(Store.getters.login_Status)
	router.beforeEach((to, from, next) => {
	    if(Store.getters.login_Status == 0 ){
	        if(Store.getters.appId == ''){
	            let res = MyHttp.post({
	                url: MyHttp.urlMap.getAppid,
	                data: {}
	            })
	            res.then(function (res) {
	                Store.commit('setLoginStatus',0)
	                Store.commit('setAppId',res.data.appid)
	                location.reload()
	            })
	            return
	        }
	        // wx8a93d5003b66a408
	        // wx90092c6556deb777  测试号
	
	        const appID=Store.getters.appId
	        // 微信未授权登录跳转到授权登录页面
	        const url = window.location.href
	        // 解决重复登录url添加重复的code与state问题
	        const parseUrl = qs.parse(url.split('?')[1]) || qs.parse(url)
	        let loginUrl
			
	        if (parseUrl.code && parseUrl.state) {
	            delete parseUrl.code
	            delete parseUrl.state
	            loginUrl = `${url.split('?')[0]}?${qs.stringify(parseUrl)}`
	        } else {
	            loginUrl = url
	        }
	        // 设置微信授权回调地址
	        let redirect_uri = loginUrl
	        redirect_uri = encodeURIComponent(redirect_uri)
	        let authUrl=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
	        // 无论拒绝还是授权都设置成1
	        
	        // 跳转到微信授权页面
			Store.commit('setLoginStatus',1)
	        window.location.href = authUrl
	    }else if (Store.getters.login_Status == 1){
	        let code = from.query.code
	        if(code){
	            let res = MyHttp.post({
	                url: MyHttp.urlMap.authUser,
	                data: {
	                    code: code
	                }
	            })
	            res.then(function (res) {
	                if(res.code == 1){
	                    let expTime = (new Date()).getTime() + config.EXPIRE*60*60*1000
						
						let userinfo = {
						    'user_id': res.data.user_id,
						    'avatarUrl': res.data.avatarUrl,
						    'nickName': res.data.nickName,
						    'expTime': expTime,
						    'token': res.data.token,
						};
						
						Store.commit('setUserInfo', {userinfo: {...userinfo, token: res.data.token}});
						
	                    Store.commit('setLoginStatus',2)
	                    next()
	                }else{
	                    Store.commit('setLoginStatus',0)
	                    location.reload()
	                }
	            })
	        }else{
	            Store.commit('setLoginStatus',0)
	            location.reload()
	        }
	    }else if (Store.getters.login_Status == 2){
			// console.log(Store.getters.user_Info)
	        if(Store.getters.user_Info.expTime && (new Date()).getTime() < Store.getters.user_Info.expTime){
	            let Info = Store.getters.user_Info
	            let expTime = (new Date()).getTime() + config.EXPIRE*60*60*1000
				
				let userinfo = {
				    'user_id': Info.user_id,
				    'avatarUrl': Info.avatarUrl,
				    'nickName': Info.nickName,
				    'expTime': expTime,
				    'token': Info.token,
				};
				Store.commit('setUserInfo', {userinfo: {...userinfo, token: Info.token}});
				
	            next();
	        }else{
				console.log('授权过期')
	            Store.commit('setLoginStatus',0)
	            location.reload()
	        }
	    }
	
	    // next();
	
	})
	router.afterEach((to, from) => {
	
	})
}
// #endif



export default RouterMount;
